package com.yixin.hospital.service.impl;

import com.yixin.hospital.common.BusinessException;
import com.yixin.hospital.entity.domain.ReportsYearEnd;
import com.yixin.hospital.entity.vo.YearEndVo;
import com.yixin.hospital.enums.ResponseCodeEnum;
import com.yixin.hospital.mapper.extend.ReportsExtendMapper;
import com.yixin.hospital.service.ReportsService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class ReportsServiceImpl implements ReportsService {

    @Resource
    private ReportsExtendMapper reportsExtendMapper;

    @Override
    public YearEndVo yearEnd(String code) {
        YearEndVo yearEnd = new YearEndVo();
        ReportsYearEnd reportsYearEnds2411 = reportsExtendMapper.paymentDetail(1, "2411", code);
        if (Objects.nonNull(reportsYearEnds2411)) yearEnd.setUsername(reportsYearEnds2411.getName());
        ReportsYearEnd reportsYearEnds2412 = reportsExtendMapper.paymentDetail(1, "2412", code);
        if (Objects.nonNull(reportsYearEnds2412)) yearEnd.setUsername(reportsYearEnds2412.getName());
        ReportsYearEnd reportsYearEnds2510 = reportsExtendMapper.paymentDetail(1, "2510", code);
        if (Objects.nonNull(reportsYearEnds2510)) yearEnd.setUsername(reportsYearEnds2510.getName());
        ReportsYearEnd reportsYearEnds2511 = reportsExtendMapper.paymentDetail(1, "2511", code);
        if (Objects.nonNull(reportsYearEnds2511)) yearEnd.setUsername(reportsYearEnds2511.getName());
        YearEndVo.PayDetail payDetail = new YearEndVo.PayDetail();
        payDetail.setName("整装");
        payDetail.setAmount1(Objects.nonNull(reportsYearEnds2411) ? reportsYearEnds2411.getPackageAmount() : BigDecimal.ZERO);
        payDetail.setAmount2(Objects.nonNull(reportsYearEnds2412) ? reportsYearEnds2412.getPackageAmount() : BigDecimal.ZERO);
        payDetail.setAmount3(Objects.nonNull(reportsYearEnds2510) ? reportsYearEnds2510.getPackageAmount() : BigDecimal.ZERO);
        payDetail.setAmount4(Objects.nonNull(reportsYearEnds2511) ? reportsYearEnds2511.getPackageAmount() : BigDecimal.ZERO);
        List<YearEndVo.PayDetail> payDetails = new ArrayList<>();
        payDetails.add(payDetail);
        YearEndVo.PayDetail payDetail2 = new YearEndVo.PayDetail();
        payDetail2.setName("检查");
        payDetail2.setAmount1(Objects.nonNull(reportsYearEnds2411) ? reportsYearEnds2411.getCheckAmount() : BigDecimal.ZERO);
        payDetail2.setAmount2(Objects.nonNull(reportsYearEnds2412) ? reportsYearEnds2412.getCheckAmount() : BigDecimal.ZERO);
        payDetail2.setAmount3(Objects.nonNull(reportsYearEnds2510) ? reportsYearEnds2510.getCheckAmount() : BigDecimal.ZERO);
        payDetail2.setAmount4(Objects.nonNull(reportsYearEnds2511) ? reportsYearEnds2511.getCheckAmount() : BigDecimal.ZERO);
        payDetails.add(payDetail2);

        YearEndVo.PayDetail total1 = new YearEndVo.PayDetail();
        total1.setName("分段合计");
        total1.setAmount1(Objects.nonNull(reportsYearEnds2411) ? reportsYearEnds2411.getPackageAmount().add(reportsYearEnds2411.getCheckAmount()) : BigDecimal.ZERO);
        total1.setAmount2(Objects.nonNull(reportsYearEnds2412) ? reportsYearEnds2412.getPackageAmount().add(reportsYearEnds2412.getCheckAmount()) : BigDecimal.ZERO);
        total1.setAmount3(Objects.nonNull(reportsYearEnds2510) ? reportsYearEnds2510.getPackageAmount().add(reportsYearEnds2510.getCheckAmount()) : BigDecimal.ZERO);
        total1.setAmount4(Objects.nonNull(reportsYearEnds2511) ? reportsYearEnds2511.getPackageAmount().add(reportsYearEnds2511.getCheckAmount()) : BigDecimal.ZERO);
        payDetails.add(total1);
        YearEndVo.PayDetail total2 = new YearEndVo.PayDetail();
        total2.setName("年合计");
        total2.setAmount1(total1.getAmount1().add(total1.getAmount2()));
        total2.setAmount2(total1.getAmount3().add(total1.getAmount4()));
        payDetails.add(total2);
        YearEndVo.PayDetail total3 = new YearEndVo.PayDetail();
        total3.setName("24-25合计");
        total3.setAmount1(total2.getAmount1().add(total2.getAmount2()));
        payDetails.add(total3);
        yearEnd.setPayDetails(payDetails);

        YearEndVo.PayDetail oldToNew = new YearEndVo.PayDetail();
        oldToNew.setName("老带新");
        List<ReportsYearEnd> reportsYearEnds23 = reportsExtendMapper.oldToNew(2, "23", code);
        if (!CollectionUtils.isEmpty(reportsYearEnds23)) {
            yearEnd.setUsername(reportsYearEnds23.get(0).getName());
            oldToNew.setAmount1(new BigDecimal(reportsYearEnds23.get(0).getPackageAmount().toString()));
        }
        List<ReportsYearEnd> reportsYearEnds24 = reportsExtendMapper.oldToNew(2, "24", code);
        if (!CollectionUtils.isEmpty(reportsYearEnds24)) {
            yearEnd.setUsername(reportsYearEnds24.get(0).getName());
            oldToNew.setAmount2(new BigDecimal(reportsYearEnds24.get(0).getPackageAmount().toString()));
        }
        List<ReportsYearEnd> reportsYearEnds25 = reportsExtendMapper.oldToNew(2, "25", code);
        if (!CollectionUtils.isEmpty(reportsYearEnds25)) {
            yearEnd.setUsername(reportsYearEnds25.get(0).getName());
            oldToNew.setAmount3(new BigDecimal(reportsYearEnds25.get(0).getPackageAmount().toString()));
        }
        yearEnd.setOldToNew(oldToNew);
        ReportsYearEnd statistics1 = reportsExtendMapper.statistics(3, code);
        if (Objects.nonNull(statistics1)) yearEnd.setUsername(statistics1.getName());
        YearEndVo.PayDetail statisticsDetail1 = new YearEndVo.PayDetail();
        statisticsDetail1.setName("应就诊月份");
        statisticsDetail1.setAmount1(BigDecimal.ZERO);
        statisticsDetail1.setAmount2(Objects.nonNull(statistics1) ? statistics1.getVisit242() : BigDecimal.ZERO);
        statisticsDetail1.setAmount3(Objects.nonNull(statistics1) ? statistics1.getVisit252() : BigDecimal.ZERO);
        YearEndVo.PayDetail statisticsDetail2 = new YearEndVo.PayDetail();
        statisticsDetail2.setName("实际就诊月份");
        statisticsDetail2.setAmount1(BigDecimal.ZERO);
        statisticsDetail2.setAmount2(Objects.nonNull(statistics1) ? statistics1.getVisit241() : BigDecimal.ZERO);
        statisticsDetail2.setAmount3(Objects.nonNull(statistics1) ? statistics1.getVisit251() : BigDecimal.ZERO);
        YearEndVo.PayDetail statisticsDetail3 = new YearEndVo.PayDetail();
        statisticsDetail3.setName("复诊月份差");
        statisticsDetail3.setAmount1(BigDecimal.ZERO);
        statisticsDetail3.setAmount2(Objects.nonNull(statistics1) ? statistics1.getVisit243() : BigDecimal.ZERO);
        statisticsDetail3.setAmount3(Objects.nonNull(statistics1) ? statistics1.getVisit253() : BigDecimal.ZERO);
        List<YearEndVo.PayDetail> statistics = new ArrayList<>();
        statistics.add(statisticsDetail1);
        statistics.add(statisticsDetail2);
        statistics.add(statisticsDetail3);
        yearEnd.setStatistics(statistics);
        if (Objects.isNull(reportsYearEnds2411) && Objects.isNull(reportsYearEnds2412) && Objects.isNull(reportsYearEnds2510) && Objects.isNull(reportsYearEnds2511)
                && reportsYearEnds23.size() == 0 && reportsYearEnds24.size() == 0 && reportsYearEnds25.size() == 0
                && Objects.isNull(statistics1)) {
            throw new BusinessException(ResponseCodeEnum.USER_NOT_EXIST.getMsg());
        }
        return yearEnd;
    }
}
